<template>
  <div>
    <el-card style="margin-bottom: 10px">
      <div class="card">
        <div class="left">
          <el-input
            placeholder="请输入教师账号"
            style="width: 180px; margin-right: 10px"
            v-model="keyword"
            clearable
            :prefix-icon="Search"
            @input="changeSeacher"
          ></el-input>
          <el-button
            type="primary"
            :icon="Search"
            @click="searchKeyword"
            v-hasBtn="'teaSelect'"
            >查询
          </el-button>
        </div>
        <div class="right">
          <el-button
            type="primary"
            :icon="Plus"
            @click="AddTeacher"
            v-hasBtn="'addTea'"
            >新增教师</el-button
          >
        </div>
      </div>
    </el-card>
    <el-card>
      <el-table border :data="teacherList" v-loading="isLoading">
        <el-table-column
          label="序号"
          align="center"
          width="80"
          type="index"
        ></el-table-column>
        <el-table-column
          label="教师账号"
          prop="teaNumber"
          align="center"
        ></el-table-column>
        <el-table-column
          label="教师姓名"
          prop="teaName"
          align="center"
        ></el-table-column>
        <el-table-column
          label="教师性别"
          prop="teaSex"
          align="center"
        ></el-table-column>
        <el-table-column label="是否在职" align="center">
          <template #default="{ row }">
            <el-tag type="success" v-if="row.teaIsWork == 0">在职</el-tag>
            <el-tag type="danger" v-else>离职</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="教师职位" align="center">
          <template #default="{ row }">
            {{ row.jobId?.jobName }}
          </template>
        </el-table-column>
        <el-table-column label="所带班级" align="center" width="170px">
          <template #default="{ row }">
            <template v-if="row.classList.length > 0">
              <el-tag
                v-for="item in row.classList"
                :key="item"
                type="success"
                style="margin: 5px 5px"
                >{{ item }}</el-tag
              >
            </template>
            <template v-else>
              <el-tag type="info">暂无班级</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center">
          <template #default="{ row }">
            {{ formatDate(row.CreateAt) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="220">
          <template #default="{ row }">
            <el-button
              type="primary"
              size="small"
              :icon="Plus"
              title="分配班级"
              @click="handleAssignClass(row._id)"
              v-hasBtn="'TeaAndClass'"
            ></el-button>
            <el-button
              type="info"
              size="small"
              :icon="Edit"
              title="修改教师"
              @click="handlerUpdateTeacher(row._id)"
              v-hasBtn="'updateTea'"
            ></el-button>
            <el-button
              type="danger"
              size="small"
              :icon="Delete"
              title="删除教师"
              @click="handlerDeleteTeacher(row._id)"
              v-hasBtn="'deleteTea'"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="page"
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20, 30, 40]"
        background
        layout=" jumper,prev, pager, next, ,total, sizes"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <AddClass
      v-model:drawer="drawer"
      :teaId="teaId"
      @getTeacherList="getTeacherList"
    ></AddClass>
    <AddOrUpdateTeacher
      v-model:teaId="teaId"
      v-model:centerDialogVisible="centerDialogVisible"
      @getTeacherList="getTeacherList"
    ></AddOrUpdateTeacher>
  </div>
</template>

<script setup>
defineOptions({
  name: "Teacher",
});
import { ref, onMounted } from "vue";
import { Search, Plus, Edit, Delete } from "@element-plus/icons-vue";
import { deleteTeacherAPI, getTeacherListAPI } from "@/api/teacher";
import { formatDate } from "@/utiles/time";
import AddClass from "./AddClass/index.vue";
import AddOrUpdateTeacher from "./AddTeacher/index.vue";
import { ElMessage, ElMessageBox } from "element-plus";
let pageNo = ref(1);
let pageSize = ref(10);
let total = ref(0);
let keyword = ref("");
let teacherList = ref([]);
let isLoading = ref(false);
let drawer = ref(false);
let teaId = ref("");
let centerDialogVisible = ref(false);
onMounted(() => {
  getTeacherList();
});
//获取教师列表
let getTeacherList = async () => {
  isLoading.value = true;
  let result = await getTeacherListAPI(
    pageNo.value,
    pageSize.value,
    keyword.value
  );
  teacherList.value = result.data.result;
  total.value = result.data.total;
  isLoading.value = false;
};
const handleSizeChange = (val) => {
  getTeacherList();
};
const handleCurrentChange = (val) => {
  getTeacherList();
};
const AddTeacher = () => {
  centerDialogVisible.value = true;
};
//更具关键字查询
const searchKeyword = () => {
  getTeacherList();
};
const changeSeacher = (val) => {
  if (val == "") {
    getTeacherList();
  }
};
//给教师分配班级
const handleAssignClass = (id) => {
  drawer.value = true;
  teaId.value = id;
};
const handlerUpdateTeacher = (id) => {
  centerDialogVisible.value = true;
  teaId.value = id;
};
const handlerDeleteTeacher = (id) => {
  ElMessageBox.confirm("此操作将永久删除该教师，是否继续?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    let result = await deleteTeacherAPI(id);
    if (result.code == 200) {
      ElMessage.success("删除成功");
      getTeacherList();
    }
  });
};
</script>

<style lang="scss" scoped>
.card {
  display: flex;
  justify-content: space-between;
}
.page {
  margin-top: 10px;
  display: flex;
  justify-content: center;
}
</style>
